//
//  WelcomeViewController.swift
//  微博模拟项目
//
//  Created by 尹家鑫 on 16/3/16.
//  Copyright © 2016年 尹家鑫. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        //大小会自动设置
        view = backImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        print(view)
    }
    
    //设置UI
    private func setupUI() {
        //添加子视图
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            
            //大小的约束
            make.height.equalTo(90)
            make.width.equalTo(90)
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
            make.centerX.equalTo(iconImageView.snp_centerX)
        }
        
        //设置layer相关的属性
        iconImageView.layer.cornerRadius = 45
        iconImageView.layer.masksToBounds = true
        iconImageView.layer.borderWidth = 3
        iconImageView.layer.borderColor = UIColor.purpleColor().CGColor
        
    }
    
    //这个方法是页面显示之后的生命周期方法.在这个方法中所有的子视图的位置/已经设置完毕
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func startAnimation() {
        //1.在动画闭包中 更新约束
        let offsetY = -UIScreen.mainScreen().bounds.height + 180
        
        //自动布局系统在这个阶段并没有修改视图的frame.这个阶段只是收集了约束的变化
        self.iconImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offsetY)
        }
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.95, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //2. 强制刷新页面 .layoutIfNeeded.这一段代码必须在动画闭包中执行
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("👌")
                        
                        //切换根视图控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                })
        }
    }
    
    //懒加载子视图
    private lazy var backImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var welcomeLabel: UILabel = {
        let l = UILabel()
        
        //设置文字
        l.text = (UserAccount.shareAccount.name ?? "") + "  欢迎归来"
        l.font = UIFont.systemFontOfSize(16)
        l.textColor = UIColor.darkGrayColor()
        l.alpha = 0
        
        l.sizeToFit()
        return l
    }()
    
}
